/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Joey Yi
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **    The GRStatusHelper provide methods to get GR status or update GR status.
 *      GR status from a status file name which start with ".SNAP-GR-Status."
 *      for example: the status file name is ".SNAP-GR-Status.MASTER", it represent the GR status is "MASTER" 
 **  
 ****************************************************************/
package com.hp.snap.gr.utils;

import com.hp.snap.gr.model.GRStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FilenameFilter;

/**
 * The GRStatusHelper provide methods to get GR status or update GR status.
 *
 * <P>
 * Version: 3.3 <br>
 * Author: Joey Yi
 * Date: Apr 10, 2015
 * 
 * </P>
 *
 **/
public class GRStatusHelper {
    public static final String GR_USER_HOME = Constants.USER_HOME + File.separator + "GR";
    private final static String GR_STATUS_FILE_PREFIX = ".SNAP-GR-Status.";
    private static Logger logger = LoggerFactory.getLogger(GRStatusHelper.class);

    public synchronized static GRStatus getGRStatus() {
        File statusFile = getGRStatusFile();

        if (statusFile == null) {
            return GRStatus.STOPPED;
        }

        String statusFileName = statusFile.getName();
        String stats = statusFileName.replace(GR_STATUS_FILE_PREFIX, "").toUpperCase();

        return GRStatus.valueOf(stats);
    }

    private static File getGRStatusFile() {

        File grUserHomeFile = new File(GR_USER_HOME);

        if (!grUserHomeFile.exists()) {
            if (!grUserHomeFile.mkdirs()) {
                logger.error("Cannot create GR User Home!");
            }
        }

        File[] statusFiles = grUserHomeFile.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.startsWith(GR_STATUS_FILE_PREFIX);
            }
        });

        File statusFile;
        if (statusFiles == null || statusFiles.length == 0) {
            statusFile = null;
        } else {
            statusFile = statusFiles[0];
        }

        return statusFile;
    }

    public synchronized static boolean updateStatus(GRStatus status) {

        File statusFile = getGRStatusFile();
        File newStatusFile = new File(GR_USER_HOME + File.separator + GR_STATUS_FILE_PREFIX + status);

        boolean result;
        if (statusFile == null) {
            statusFile = newStatusFile;
            result = FileUtils.createFile(statusFile.getPath());
        } else {
            result = statusFile.renameTo(newStatusFile);
        }

        return result;
    }
}
